import 'package:love_app/common/su.dart';
import 'package:love_app/common/su_dio.dart';
import 'package:love_app/config/path_url.dart';
import 'package:love_app/model/comment_detailst_model.dart';
import 'package:love_app/model/dynamic_details_model.dart';
import 'package:love_app/model/response_model.dart';
import 'package:love_app/service/base_api.dart';

class CommentDetailsApi extends BaseApi {
  // 通过id获取所有评论
  Future<CommentDetailstModel> getAllCommentDetailsByItemId(
      String token, int itemId, {int page = 1}) async {
    Map header = {
      'X-Auth-Token': token,
    };
    String path = PathUrl.commentDetails + itemId.toString() + "/page/" + page.toString();
    ResponseModel responseModel =
        await SuDio().getRequest(path, headersMap: header);
    CommentDetailstModel commentDetailstModel =
        CommentDetailstModel.fromJson(responseModel.toJsonModel);
    return commentDetailstModel;
  }

  // 回复评论接口
  Future<int> replyCommentsById(
      String token, String content, int itemID, int cId) async {
    Map header = {
      'X-Auth-Token': token,
    };
    Map postData = {
      'content': content,
      'd_id': itemID,
      'c_id': cId == -1 ? null : cId,
    };
    String path = PathUrl.commentsCreate;
    final ResponseModel responseModel =
        await SuDio().postRequest(path, headersMap: header, data: postData);
    int id = responseModel.data['data']['id'];
    return id;
  }

  // 根据id删除社区文章的评论
  Future<bool> deleteCommentsById(String token, int itemID, int cId)async{
    Map header = {
      'X-Auth-Token': token,
    };
    Map postData = {
      'd_id': itemID,
      'c_id': cId == -1 ? null : cId,
    };
    String path = PathUrl.deleteComments;
    final ResponseModel responseModel =
        await SuDio().postRequest(path, headersMap: header, data: postData);
    return isSuccess(responseModel.data);
  }

  // 根据id关注用户
  Future<bool> followersUser(String token, int userId)async{
    Map header = {
      'X-Auth-Token': token,
    };
    String path = PathUrl.followers + userId.toString();
    final ResponseModel responseModel = await SuDio().getRequest(path, headersMap: header);
    Su.suPrints(responseModel.data);
    return isSuccess(responseModel.data);
  }

  // 根据id关注用户
  Future<bool> deleteDynamic(String token, int itemId)async{
    Map header = {
      'X-Auth-Token': token,
    };
    Map postData = {
      'd_id': itemId,
    };
    String path = PathUrl.deleteDynamic;
    final ResponseModel responseModel = await SuDio().postRequest(path, headersMap: header, data: postData);
    return isSuccess(responseModel.data);
  }

  // 根据文章id获取文章详情
  Future<DynamicDetailsModel> getDynamicDetails(String token, int itemId)async{
    String path = PathUrl.dynamicDetails + "$itemId";
    ResponseModel responseModel = await SuDio().getRequest(path, headersMap: headerToken(token));
    return DynamicDetailsModel.fromJson(responseModel.toJsonModel);
  }
}
